package leetcode.median;

import leetcode.base.TreeNode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * ClassName: $102_LevelOrder
 * Description:
 * date: 2022-01-30 16:52
 *
 * @author liyifan
 */
public class $102_LevelOrder {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        if (root == null) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int n = queue.size();
            List<Integer> path = new LinkedList<>();
            for (int i = 0;i < n;i++) {
                TreeNode poll = queue.poll();
                path.add(poll.val);
                if (poll.left != null)
                    queue.add(poll.left);
                if (poll.right != null)
                    queue.add(poll.right);
            }
            res.add(path);
        }
        return res;
    }
}
